<?php
include_once('../../php/postgre.php');

$act = '';
if(isset($_POST['act'])) $act = $_POST['act'];
if($act=='tahun_action'){
	$id_thn_angg = $_POST['id_thn_angg'];
	$thn_angg = $_POST['thn_angg'];
	
	if(!is_numeric($id_thn_angg)){
		echo "Err: invalid id.";
		exit;
	}
	
	if($id_thn_angg>0){
		exec_query("update spm_thn_angg set thn_angg='".$thn_angg."' where id_thn_angg=".$id_thn_angg);
		echo 'success__';
	}else{
		$maxid = get_maxid('id_thn_angg','spm_thn_angg');
		exec_query("insert into spm_thn_angg(id_thn_angg, thn_angg) values ('".$maxid."','".$thn_angg."')");
		$stored = get_data('select id_thn_angg from spm_thn_angg where id_thn_angg='.$maxid);
		if($stored['id_thn_angg']==$maxid){
			echo 'success__';
			$id_thn_angg = $maxid;
		}
	}
	
	echo $id_thn_angg.'__';
	view_thn_angg();
	
}else if($act=='del_thn_angg'){
	$id_thn_angg = $_POST['id_thn_angg'];
	if(is_numeric($id_thn_angg)){
		exec_query("delete from spm_thn_angg where id_thn_angg=".$id_thn_angg);
		echo 'success__';
	}
	view_thn_angg();
}else if($act=='edit_thn_angg'){
	$id_thn_angg = $_POST['id_thn_angg'];
	if(is_numeric($id_thn_angg)){
		$data = get_data("select * from spm_thn_angg where id_thn_angg =".$id_thn_angg);
		$ress = implode($data,'__');
		echo $ress;
		 echo "__";
	}else{
		echo 'error';
	}
	}

function view_thn_angg(){
					$no=1;
					$thn_angg=get_datas("SELECT * FROM spm_thn_angg");
					foreach($thn_angg as $thn_angg){
					?>
						<tr>
							<td><?php echo $no ?></td>
							<td style="text-align:center;"><?php echo $thn_angg['thn_angg']?></td>	
							<td style="text-align:center; width:200px;">
							<a class="btn btn-danger bt-delete-thn_angg" name="<?php echo $thn_angg['id_thn_angg']; ?>">Delete</a>
                                <a  href="#modalwin" data-toggle="modal"  class="btn btn-info bt-edit-thn_angg" name="<?php echo $thn_angg['id_thn_angg']; ?>">Edit</a>
							</td>
							
						</tr>
						<?php 
						$no++;
						}
						}
						?>
						